package org.dsc.amafinder;

import org.dsc.amafinder.model.AmfQR;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AOptions extends Activity {
   private static final String TAG = "afr." + AOptions.class.getSimpleName();
   private static final String PAIS = "PAIS";
   
   private SharedPreferences mShPrefs; 
   private ListView paisesListView;
   private Button btnPais;
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Log.i(TAG, "onCreate::" + toString());
      setContentView(R.layout.aoptions);
      
      mShPrefs = getPreferences(MODE_PRIVATE);
      String pais = mShPrefs.getString(PAIS, AmfQR.pais);
      Toast.makeText(getApplicationContext(), pais, Toast.LENGTH_SHORT).show();
      
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources()
               .getStringArray(R.array.paises));
      paisesListView = (ListView) findViewById(R.id.Paises);
      paisesListView.setAdapter(adapter);
      paisesListView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, View view,
               int position, long id) {
            String pais = ((TextView) view).getText().toString();
            Toast.makeText(getApplicationContext(), pais, Toast.LENGTH_SHORT).show();
            AmfQR.pais = "es";
         }
      });

      btnPais = (Button) findViewById(R.id.btnPais);
      btnPais.setOnClickListener(new OnClickListener() {
         @Override
         public void onClick(View v) {
            saveAndExit();
         }
      });
   }
   
   @Override
   protected void onResume() {
      super.onResume();
      Log.i(TAG, "onResume::" + toString());      
   }

   @Override
   protected void onRestart() {
      super.onRestart();
      Log.i(TAG, "onRestart::" + toString());
   }   
   
   protected void saveAndExit() {
      SharedPreferences.Editor editor = mShPrefs.edit();
      editor.putString(PAIS, AmfQR.pais);
      editor.commit();
      finish();
   }

   /*
    * @ini-menu-action-bar
    */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      getMenuInflater().inflate(R.menu.main_activity_actions, menu);
      menu.findItem(R.id.action_search).setIntent(new Intent(this, AMain.class));
//      menu.findItem(R.id.action_options).setIntent(new Intent(this, AOptions.class));
      menu.findItem(R.id.action_info).setIntent(new Intent(this, AInfo.class));
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_search:
      case R.id.action_info:
         saveAndExit();
         startActivity(item.getIntent());
         break;
      default:
         return super.onOptionsItemSelected(item);
      }
      return true;
   }
   /*
    * @end-menu-action-bar
    */   
}
